package com.jl.tools.util;

import java.io.IOException;

import de.cardcontact.opencard.utils.CapFile;

public class CapFileInfo {
	public final static String[] COMPONENTS = { "HEADER", "DIRECTORY",
			"APPLET", "IMPORT", "CONSTANTPOOL", "CLASS", "METHOD",
			"STATICFIELD", "REFLOCATION", "EXPORT", "DESCRIPTOR", "DEBUG" };
	private CapFile capfile;
	private boolean successRead = false;
	private String name;

	public CapFileInfo(String fullname) {
		this.capfile = new CapFile(fullname);
		this.name = fullname;
		try {
			capfile.read();
		} catch (IOException e) {
			successRead = false;
		}
		successRead = true;
	}

	public String getInfomation() {
		if (!successRead) {
			return "read cap file failed : " + name;
		}
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < COMPONENTS.length; i++) {
			sb.append(getOneComponents(i)).append("\n");
		}
		return sb.toString();
	}

	private String getOneComponents(int i) {
		StringBuilder sb = new StringBuilder();
		sb.append(COMPONENTS[i]).append(" : ")
				.append(capfile.getLoadFile(new int[] { i }).length)
				.append(" Bytes");
		return sb.toString();
	}

}
